require 'View/UIBase/UIBase'

UICommonLevelUp = UIBase.New(PanelNames.UICommonLevelUp)

function UICommonLevelUp.Init(before, after, fun, LevelUpType)
    UICommonLevelUp.beforeLevel = before;
    UICommonLevelUp.afterLevel = after;
    UICommonLevelUp.callbackFun = fun;
    UICommonLevelUp.levelUpType = LevelUpType;
    UIBase.Init(UICommonLevelUp,func);
end

-- 创建回调
function UICommonLevelUp:OnCreate()
    if self.haveInit ~= true then
        self.haveInit = true
        self.levelBefore = self:GetChild("levelBefore");
        self.levelAfter = self:GetChild("levelAfter");
        self.list = self:GetChild("list");
        self.listInfo = self:GetChild("List");
        self.txtInfo = self:GetChild("Tips");
        self.txtTitle = self:GetChild("n23");
        self.txtLevel = self:GetChild("n24");
        self.txtGoReward = self:GetChild("TipsShow");
        self.typeController = self:GetController("LevelUp");
        self:RegisterOnClick(self:GetChild("clickPanel"),self.OnClose);
    end

    self:InitData()
end

-- 显示
function UICommonLevelUp:InitData()
    if self.levelUpType == nil or self.levelUpType == EnumConst.LevelUpType.Character then
        self.typeController.selectedIndex = 0    -- 0-主角升级；1-Vip升级
    elseif self.levelUpType == EnumConst.LevelUpType.Vip then
        self.typeController.selectedIndex = 1
    end
    if self.beforeLevel == nil or self.afterLevel == nil then
        self:OnClose();
    else
        self:Refresh();
    end
end

function UICommonLevelUp:Refresh()
    self:UpdateData();
end

function UICommonLevelUp:UpdateData()
    self.levelBefore.text = self.beforeLevel;
    self.levelAfter.text = self.afterLevel;
    self.rewardList = {};
    if self.levelUpType == nil or self.levelUpType == EnumConst.LevelUpType.Character then
        local data = {};
        for i = self.beforeLevel, self.afterLevel - 1 do
            self.levelConfig = G.dataTable["t_characterexp"][MakeTopLevelId(i)];
            if self.levelConfig ~= nil then
                for k, v in pairs(decodeJsonStrToTabel(self.levelConfig.f_LevelUpReward)) do
                    if data[v[1]] then
                        self.rewardList[data[1]][2] = self.rewardList[data[1]][2] + v[2];
                    else
                        table.insert(self.rewardList, {v[1], v[2]});
                        data[v[1]] = #self.rewardList;
                    end
                end
            end
        end
    elseif self.levelUpType == EnumConst.LevelUpType.Vip then
        self.txtInfo.text = string.format(getLanguage("VipTips002"),self.afterLevel);
        self.txtTitle.text = getLanguage("VipTips004");
        self.txtLevel.text = getLanguage("VipTips005");
        self.txtGoReward.text = getLanguage("VipTips006");
        local data, isReward = logicMgr.VipManager.GetVipTableDataByLv(self.afterLevel);
        if data ~= nil then
            self.rewardList = decodeJsonStrToTabel(data.f_VipReward)
        end
        self.listInfo:RemoveChildrenToPool();
        if data.f_VipBuff ~= "" then
            for k, v in pairs(decodeJsonStrToTabel(data.f_VipBuff)) do
                local item = self.listInfo:AddItemFromPool();
                local itemInfo = item:GetChild("info");
                local buff = G.dataTable["t_ecobuff"][tostring(v)];
                if buff ~= nil then
                    itemInfo.text = getLanguage(buff.f_BuffDesc);
                else
                    itemInfo.text = "";
                end
            end
        end
        if data.f_VipFuncSwitchDesc ~= "" then
            for k, v in pairs(decodeJsonStrToTabel(data.f_VipFuncSwitchDesc)) do
                local item = self.listInfo:AddItemFromPool();
                local itemInfo = item:GetChild("info");
                itemInfo.text = getLanguage(v, nil , "");
            end
        end
    end

    logicMgr.CostManager.RefreshItemList(self.list, self.rewardList);
    --玩家升级音效
    AudioManager.Play(2014)
end

-- 关闭
function UICommonLevelUp:OnClose() 
    self:OnBack();
    if self.callbackFun ~= nil then
        self.callbackFun();
    end
--角色升级获得道具音效
    for _, itemModelId in ipairs(self.rewardList) do
        AudioManager.TryPlaySoundItemChange(itemModelId[1],true)
    end
end

-- 隐藏
function UICommonLevelUp:OnDisable()
end

-- 卸载
function UICommonLevelUp:OnDestroy()
    self.haveInit = nil
    self.levelBefore = nil
    self.levelAfter = nil
    self.list = nil
    self.listInfo = nil
    self.txtInfo = nil
    self.txtTitle = nil
    self.txtLevel = nil
    self.txtGoReward = nil
    self.typeController = nil
end

return UICommonLevelUp